package com.gralves.elasticsearch.utils.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zhouyibin
 * @date 2018/12/18
 * @desc
 */
@Slf4j
@EnableAsync
@Configuration
public class ThreadPoolConfig {

    @Bean
    @SuppressWarnings("all")
    public ThreadPoolTaskExecutor executor(){
        log.warn(">>>>>>>>>>>>>>>>>>[ initial ThreadPool ......     ]<<<<<<<<<<<<<<<<");
        ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        log.warn(">>>>>>>>>>>>>>>>>>[ CorePoolSize : 20             ]<<<<<<<<<<<<<<<<");
        executor.setAllowCoreThreadTimeOut(true);
        log.warn(">>>>>>>>>>>>>>>>>>[ AllowCoreThreadTimeOut : true ]<<<<<<<<<<<<<<<<");
        executor.setKeepAliveSeconds(60);
        log.warn(">>>>>>>>>>>>>>>>>>[ KeepAliveSeconds : 60         ]<<<<<<<<<<<<<<<<");
        executor.setMaxPoolSize(30);
        log.warn(">>>>>>>>>>>>>>>>>>[ MaxPoolSize : 30              ]<<<<<<<<<<<<<<<<");
        executor.setQueueCapacity(200);
        log.warn(">>>>>>>>>>>>>>>>>>[ QueueCapacity : 200           ]<<<<<<<<<<<<<<<<");
        executor.setThreadNamePrefix("sync-task-");
        log.warn(">>>>>>>>>>>>>>>>>>[ ThreadNamePrefix : sync-task- ]<<<<<<<<<<<<<<<<");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        log.warn(">>>>>>>>>>>>>>>>>>[ RejectedExecutionHandler : CallerRunsPolicy ]<<<<<<<<<<<<<<<<");
        executor.setWaitForTasksToCompleteOnShutdown(true);
        log.warn(">>>>>>>>>>>>>>>>>>[ WaitForTasksToCompleteOnShutdown : true ]<<<<<<<<<<<<<<<<");
        executor.setAwaitTerminationSeconds(60);
        log.warn(">>>>>>>>>>>>>>>>>>[ AwaitTerminationSeconds : 60  ]<<<<<<<<<<<<<<<<");
        executor.initialize();
        log.warn(">>>>>>>>>>>>>>>>>>[ finish initial ThreadPool ...... ]<<<<<<<<<<<<<<<<");
        return executor;
    }
}
